//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "YoYInflationCoupon.h"
using namespace Cephei::QL::Cashflows;
#include <gen/QL/Indexes/YoYInflationIndex.h>
#include <gen/QL/Times/Period.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Indexes/InflationIndex.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/Cashflows/InflationCouponPricer.h>
#include <gen/QL/Cashflows/InflationCoupon.h>
using namespace Cephei::QL::Indexes;
using namespace Cephei::QL::Times;
using namespace Cephei::QL::Termstructures;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Cashflows::CYoYInflationCoupon::CYoYInflationCoupon (DateTime paymentDate, Double nominal, DateTime startDate, DateTime endDate, UInt32 fixingDays, Cephei::QL::Indexes::IYoYInflationIndex^ index, Cephei::QL::Times::IPeriod^ observationLag, Cephei::QL::Times::IDayCounter^ dayCounter, Microsoft::FSharp::Core::FSharpOption<Double>^ gearing, Microsoft::FSharp::Core::FSharpOption<Double>^ spread, Microsoft::FSharp::Core::FSharpOption<DateTime>^ refPeriodStart, Microsoft::FSharp::Core::FSharpOption<DateTime>^ refPeriodEnd, Cephei::QL::Cashflows::IInflationCouponPricer^ QL_Pricer) : CInflationCoupon(CYoYInflationCoupon::typeid)
{
    CYoYInflationIndex^ _Cindex;
    CPeriod^ _CobservationLag;
    CDayCounter^ _CdayCounter;
    try
    {
#ifdef HANDLE
        _phYoYInflationCoupon = NULL;
#endif
        QuantLib::Date _paymentDate = (QuantLib::Date)ValueHelper::Convert (paymentDate);
        QuantLib::Real _nominal = (QuantLib::Real)ValueHelper::Convert (nominal);
        QuantLib::Date _startDate = (QuantLib::Date)ValueHelper::Convert (startDate);
        QuantLib::Date _endDate = (QuantLib::Date)ValueHelper::Convert (endDate);
        QuantLib::Natural _fixingDays = (QuantLib::Natural)ValueHelper::Convert (fixingDays);
        _Cindex = safe_cast<CYoYInflationIndex^> (index);
        _Cindex->Lock();
        boost::shared_ptr<QuantLib::YoYInflationIndex>& _index = static_cast<boost::shared_ptr<QuantLib::YoYInflationIndex>&> (_Cindex->GetShared ()); 
        _CobservationLag = safe_cast<CPeriod^> (observationLag);
        _CobservationLag->Lock();
        QuantLib::Period& _observationLag = static_cast<QuantLib::Period&> (_CobservationLag->GetReference ()); 
        _CdayCounter = safe_cast<CDayCounter^> (dayCounter);
        _CdayCounter->Lock();
        QuantLib::DayCounter& _dayCounter = static_cast<QuantLib::DayCounter&> (_CdayCounter->GetReference ()); 
        QuantLib::Real _gearing = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (gearing) ? (QuantLib::Real)ValueHelper::Convert (gearing->Value) : 1.0); //4
        QuantLib::Spread _spread = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (spread) ? (QuantLib::Spread)ValueHelper::Convert (spread->Value) : 0.0); //4
        QuantLib::Date _refPeriodStart = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (refPeriodStart) ? (QuantLib::Date)ValueHelper::Convert (refPeriodStart->Value) : QuantLib::Date()); //4
        QuantLib::Date _refPeriodEnd = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (refPeriodEnd) ? (QuantLib::Date)ValueHelper::Convert (refPeriodEnd->Value) : QuantLib::Date()); //4
        _ppYoYInflationCoupon = new boost::shared_ptr<QuantLib::YoYInflationCoupon> (new QuantLib::YoYInflationCoupon ( _paymentDate,  _nominal,  _startDate,  _endDate,  _fixingDays,  _index,  _observationLag,  _dayCounter,  _gearing,  _spread,  _refPeriodStart,  _refPeriodEnd ));
        CInflationCouponPricer^ _CQL_Pricer = safe_cast<CInflationCouponPricer^> (QL_Pricer);
        boost::shared_ptr<QuantLib::InflationCouponPricer>& _QL_Pricer = static_cast<boost::shared_ptr<QuantLib::InflationCouponPricer>&> (_CQL_Pricer->GetShared ());
        (*_ppYoYInflationCoupon)->setPricer (_QL_Pricer);
        SetInflationCoupon (boost::dynamic_pointer_cast<QuantLib::InflationCoupon> (*_ppYoYInflationCoupon));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cindex != nullptr) _Cindex->Unlock();
        if (_CobservationLag != nullptr) _CobservationLag->Unlock();
        if (_CdayCounter != nullptr) _CdayCounter->Unlock();
    }
}
Cephei::QL::Cashflows::CYoYInflationCoupon::CYoYInflationCoupon (boost::shared_ptr<QuantLib::YoYInflationCoupon>& childNative, Object^ owner) : CInflationCoupon(CYoYInflationCoupon::typeid)
{
#ifdef HANDLE
	_phYoYInflationCoupon = NULL;
#endif
	_ppYoYInflationCoupon = &childNative;
    _ppInflationCoupon = new boost::shared_ptr<QuantLib::InflationCoupon> (boost::dynamic_pointer_cast<QuantLib::InflationCoupon> (*_ppYoYInflationCoupon));
}
Cephei::QL::Cashflows::CYoYInflationCoupon::CYoYInflationCoupon (QuantLib::YoYInflationCoupon& childNative, Object^ owner) : CInflationCoupon(CYoYInflationCoupon::typeid)
{
#ifdef HANDLE
	_phYoYInflationCoupon = NULL;
#endif
	_ppYoYInflationCoupon = new boost::shared_ptr<QuantLib::YoYInflationCoupon> (&childNative);
    _ppInflationCoupon = new boost::shared_ptr<QuantLib::InflationCoupon> (boost::dynamic_pointer_cast<QuantLib::InflationCoupon> (*_ppYoYInflationCoupon));
    _YoYInflationCouponOwner = owner;
    _InflationCouponOwner = owner;
}

Cephei::QL::Cashflows::CYoYInflationCoupon::CYoYInflationCoupon (CYoYInflationCoupon^ copy) : CInflationCoupon(CYoYInflationCoupon::typeid)
{
#ifdef HANDLE
	_phYoYInflationCoupon = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppYoYInflationCoupon = new boost::shared_ptr<QuantLib::YoYInflationCoupon> (copy->GetShared());
        _ppInflationCoupon = new boost::shared_ptr<QuantLib::InflationCoupon> (boost::dynamic_pointer_cast<QuantLib::InflationCoupon> (*_ppYoYInflationCoupon));
    }
}
Cephei::QL::Cashflows::CYoYInflationCoupon::CYoYInflationCoupon (System::Type^ t) : CInflationCoupon(CYoYInflationCoupon::typeid)
{
#ifdef HANDLE
	_phYoYInflationCoupon = NULL;
#endif
	if (!t->IsSubclassOf(CYoYInflationCoupon::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Cashflows::CYoYInflationCoupon::CYoYInflationCoupon (QuantLib::Handle<QuantLib::YoYInflationCoupon>& childNative, Object^ owner)  : CInflationCoupon(CYoYInflationCoupon::typeid)
{
	_phYoYInflationCoupon = &childNative;
	_ppYoYInflationCoupon = &static_cast<boost::shared_ptr<QuantLib::YoYInflationCoupon>>(childNative.currentLink());
    _ppInflationCoupon = new boost::shared_ptr<QuantLib::InflationCoupon> (boost::dynamic_pointer_cast<QuantLib::InflationCoupon> (*_ppYoYInflationCoupon));
    _YoYInflationCouponOwner = owner;
}
Cephei::QL::Cashflows::CYoYInflationCoupon::CYoYInflationCoupon (QuantLib::Handle<QuantLib::YoYInflationCoupon> childNative)  : CInflationCoupon(CYoYInflationCoupon::typeid)
{
	_phYoYInflationCoupon = &childNative;
	_ppYoYInflationCoupon = &static_cast<boost::shared_ptr<QuantLib::YoYInflationCoupon>>(childNative.currentLink());
    _ppInflationCoupon = new boost::shared_ptr<QuantLib::InflationCoupon> (boost::dynamic_pointer_cast<QuantLib::InflationCoupon> (*_ppYoYInflationCoupon));
}
#endif
#ifdef STRUCT
Cephei::QL::Cashflows::CYoYInflationCoupon::CYoYInflationCoupon (QuantLib::YoYInflationCoupon childNative)  : CInflationCoupon(CYoYInflationCoupon::typeid)
{
#ifdef HANDLE
	_phYoYInflationCoupon = NULL;
#endif
	_ppYoYInflationCoupon = new boost::shared_ptr<QuantLib::YoYInflationCoupon> (new QuantLib::YoYInflationCoupon (childNative));
    _ppInflationCoupon = new boost::shared_ptr<QuantLib::InflationCoupon> (boost::dynamic_pointer_cast<QuantLib::InflationCoupon> (*_ppYoYInflationCoupon));
}
#endif

Cephei::QL::Cashflows::CYoYInflationCoupon::~CYoYInflationCoupon ()
{
    if (_ppYoYInflationCoupon != NULL)
    {
	    delete _ppYoYInflationCoupon;
        _ppYoYInflationCoupon = NULL;
    }
}
Cephei::QL::Cashflows::CYoYInflationCoupon::!CYoYInflationCoupon ()
{
    if (_ppYoYInflationCoupon != NULL)
    {
	    delete _ppYoYInflationCoupon;
    }
}
QuantLib::YoYInflationCoupon& Cephei::QL::Cashflows::CYoYInflationCoupon::GetReference ()
{
    if (_ppYoYInflationCoupon == NULL) throw gcnew NativeNullException ();
	return **_ppYoYInflationCoupon;
}
boost::shared_ptr<QuantLib::YoYInflationCoupon>& Cephei::QL::Cashflows::CYoYInflationCoupon::GetShared ()
{
    if (_ppYoYInflationCoupon == NULL) throw gcnew NativeNullException ();
	return *_ppYoYInflationCoupon;
}
QuantLib::YoYInflationCoupon* Cephei::QL::Cashflows::CYoYInflationCoupon::GetPointer ()
{
    if (_ppYoYInflationCoupon == NULL) throw gcnew NativeNullException ();
	return &**_ppYoYInflationCoupon;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::YoYInflationCoupon>& Cephei::QL::Cashflows::CYoYInflationCoupon::GetHandle ()
{
	if (_phYoYInflationCoupon == NULL)
	{
		_phYoYInflationCoupon = new Handle<QuantLib::YoYInflationCoupon> (*_ppYoYInflationCoupon);
	}
	return *_phYoYInflationCoupon;
}
#endif
bool Cephei::QL::Cashflows::CYoYInflationCoupon::HasNative () 
{
	return (_ppYoYInflationCoupon != NULL);
}

Double Cephei::QL::Cashflows::CYoYInflationCoupon::AdjustedFixing::get ()
{
    try
    {
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppYoYInflationCoupon)->adjustedFixing ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Cashflows::CYoYInflationCoupon::Gearing::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppYoYInflationCoupon)->gearing ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Cashflows::CYoYInflationCoupon::Spread::get ()
{
    try
    {
    	QuantLib::Spread _rv = (QuantLib::Spread)(*_ppYoYInflationCoupon)->spread ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Indexes::IYoYInflationIndex^ Cephei::QL::Cashflows::CYoYInflationCoupon::YoyIndex::get ()
{
    try
    {
    	boost::shared_ptr<QuantLib::YoYInflationIndex>& _rv = (boost::shared_ptr<QuantLib::YoYInflationIndex>&)(*_ppYoYInflationCoupon)->yoyIndex ( );   
        Cephei::QL::Indexes::CYoYInflationIndex^ _nrv = gcnew Cephei::QL::Indexes::CYoYInflationIndex (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Cashflows::IYoYInflationCoupon^ Cephei::QL::Cashflows::CYoYInflationCoupon_Factory::Create (DateTime paymentDate, Double nominal, DateTime startDate, DateTime endDate, UInt32 fixingDays, Cephei::QL::Indexes::IYoYInflationIndex^ index, Cephei::QL::Times::IPeriod^ observationLag, Cephei::QL::Times::IDayCounter^ dayCounter, Microsoft::FSharp::Core::FSharpOption<Double>^ gearing, Microsoft::FSharp::Core::FSharpOption<Double>^ spread, Microsoft::FSharp::Core::FSharpOption<DateTime>^ refPeriodStart, Microsoft::FSharp::Core::FSharpOption<DateTime>^ refPeriodEnd, Cephei::QL::Cashflows::IInflationCouponPricer^ QL_Pricer)
{
    return gcnew CYoYInflationCoupon ( paymentDate,  nominal,  startDate,  endDate,  fixingDays,  index,  observationLag,  dayCounter,  gearing,  spread,  refPeriodStart,  refPeriodEnd,  QL_Pricer);
}
